<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$return=array();

if($_RQ['op']=='edit'){
	
    $return['coupon']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['status']=array('1'=>'上架','0'=>'下架');
	$return['type']=array('1'=>'选择会员','2'=>'全部发送');
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		
		'desc'=>'优惠券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'发送设置',
				'list'=>array(
					
					array('type'=>'select','name'=>'coupon','title'=>'选择优惠券','options'=>'coupon'),
				
					array('type'=>'string','name'=>'num','title'=>'发放数量','help'=>'此处受总数限制，如果剩余张数不足以发放给选定会员数量，则无法发放'),
					array('type'=>'radio','name'=>'type','title'=>'发送用户','options'=>'type'),
					array('type'=>'text','name'=>'mids','title'=>'选择用户','show'=>array('type'=>1),'help'=>'填写会员id，以英文逗号分隔 如 1,2'),
		)
	),
		
			array(
				'title'=>'模板消息设置',
				'list'=>array(
					array('type'=>'string','name'=>'templateid','title'=>'模板id','help'=>'模版搜索关键字：任务处理通知/OPENTM200605630'),
					array('type'=>'string','name'=>'frist','title'=>'头部信息','help'=>'例如：优惠券通知'),
					array('type'=>'color','name'=>'fristcolor','title'=>'头部颜色'),
					array('type'=>'string','name'=>'keyword1','title'=>'通知信息1','help'=>'例如：恭喜您获得优惠卷'),
					array('type'=>'color','name'=>'keyword1color','title'=>'通知信息1颜色'),
					array('type'=>'string','name'=>'keyword2','title'=>'通知信息2','help'=>'例如：请您点击查看'),
					array('type'=>'color','name'=>'keyword2color','title'=>'通知信息2颜色'),
					
									
				)
			),
			
	)
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$data=get_postdata();
	$msg = array( 'first' => array('value' => $data['frist'], 'color' => $data['fristcolor']), 'remark' => array('value' => $data['remark'], 'color' => $data['remarkcolor']) );
	$msg['keyword1'] = array('value' => $data['keyword1'], 'color' => $data['keyword1color']);
	$msg['keyword2'] = array('value' => $data['keyword2'], 'color' => $data['keyword2color']);
	if($data['type']=1){
		$member=explode(',',$data['mids']);
	}else{
		$user=pdo_getall('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid']),array('mid'));
		$member=array();
		foreach($user as $v){
			$member[]=$v['mid'];	
		}
	}
	foreach($member as $m){
		$data1=array('mid'=>$m,'status'=>0,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'getfrom'=>0,'createtime'=>TIMESTAMP);
		$data2=array('mid'=>$m,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'createtime'=>TIMESTAMP);
		pdo_update('jy_weishop_coupon',array('last +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$data['coupon']));
		pdo_insert('jy_weishop_coupon_getlog',$data2);
		pdo_insert('jy_weishop_coupon_log',$data1);
		cfc('account')->sendTplNotice($m, $data['templateid'], $msg, $url =$data['coupon'], $topcolor = '#FF683F');
	}
	exi('发放成功','');
}
?>